package com.tgy.leetcode;

import java.util.Stack;

/**
 * @Author: tgy
 * @Date: 12/1/20 9:33 AM
 */
public class _面试题_03_02_栈的最小值 {

    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();


    public void push(int x) {

        stack.push(x);

        if (minStack.isEmpty()) {

            minStack.push(x);
        }else {

            minStack.push(Math.min(x,minStack.peek()));
        }

    }

    public void pop() {

        if (stack.isEmpty()) {

            return;
        }

        stack.pop();
        minStack.pop();
    }

    public int top() {

        return stack.peek();
    }

    public int getMin() {

        return minStack.peek();
    }

}
